﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace WebClass
{

    public class Task
    {

        public int TaskID { get; set; }
        public string TaskName { get; set; }
        public int TaskDontComplete { get; set; }
        public int SumTask { get; set; }
        public int ItemCountInTask { get; set; }
        ResearchDBDataContext researchDB;
        public Task()
        {
            researchDB = new ResearchDBDataContext();
        }
        public List<Task> GetTask(int userID)
        {
         
            List<Task> tasks = new List<Task>();
            var experimentsResult = from t in researchDB.tb_tasks
                                    from e in t.tb_experiments
                                    where e.userID == userID
                                    group e by e.taskID into g
                                    select g;
            foreach (var exps in experimentsResult)
            {
                Task task = new Task();
                int currentTaskDontComplete = 0;
                int currentTaskSumTask = 0;
                foreach (var item in exps)
                {
                    if (currentTaskSumTask == 0)
                    {
                        task.ItemCountInTask = (from t in researchDB.tb_experiments where t.taskID == item.tb_task.ID select t).Count();
                        task.TaskID = item.tb_task.ID;
                        task.TaskName = item.tb_task.name;
                        task.TaskCreateTime = item.tb_task.createTime;
                    }
                    if (item.isComplete == false)
                    {
                        ++currentTaskDontComplete;
                    }
                    ++currentTaskSumTask;
                }
                task.TaskDontComplete = currentTaskDontComplete;
                task.SumTask = currentTaskSumTask;
                tasks.Add(task);
            }
            return tasks;
        }
        public DateTime? TaskCreateTime { get; set; }
    }
}
